package cn.edu.nchu.fragmentdemo.fragments;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import androidx.fragment.app.Fragment;

import cn.edu.nchu.fragmentdemo.R;
import cn.edu.nchu.fragmentdemo.view.PostureBallView;


/**
 * @author DELL
 */
public class UavPostureBallFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        final View rootView = inflater.inflate(R.layout.fragment_uav_postureball, container, false);

        // 找到PostureBallView的实例
        PostureBallView postureBallView = rootView.findViewById(R.id.fragment_posture_ball);

        //添加图标点击监听
        final ImageButton imageButton = rootView.findViewById(R.id.imageButton);
        imageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 切换图片
                if ( imageButton.isSelected() ) {
                    imageButton.setImageResource(R.drawable.posture_visible_fragment_flag);
                    PostureBallView postureBallView = getView().findViewById(R.id.posture_ball_view);
                    postureBallView.setVisibility(View.VISIBLE);
                    FlightInformation flightInformation = getView().findViewById(R.id.posture_ball_flight_information);
                    flightInformation.setVisibility(View.VISIBLE);
                } else {
                    imageButton.setImageResource(R.drawable.posture_invisible_fragment_flag);
                    PostureBallView postureBallView1 = getView().findViewById(R.id.posture_ball_view);
                    postureBallView1.setVisibility(View.GONE);
                    FlightInformation flightInformation = getView().findViewById(R.id.posture_ball_flight_information);
                    flightInformation.setVisibility(View.GONE);
                }
                imageButton.setSelected(!imageButton.isSelected());

            }
        });

        //对整个视图添加拖动监听
        rootView.setOnTouchListener(new View.OnTouchListener() {
            private float ballX;
            private float ballY;

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                float startX = event.getRawX();
                float startY = event.getRawY();
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        ballX = startX;
                        ballY = startY;
                        break;
                    case MotionEvent.ACTION_MOVE:
                        float offsetX = startX - ballX;
                        float offsetY = startY - ballY;
                        ballX = startX;
                        ballY = startY;
                        updateBallPosition(offsetX, offsetY,rootView);
                        break;
                }
                return true;
            }
        });
        return rootView;
    }

    /** 位置更新函数 */
    private void updateBallPosition(float offsetX, float offsetY,View rootView) {
        ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) rootView.getLayoutParams();
        int leftMargin = layoutParams.leftMargin + (int) offsetX;
        int topMargin = layoutParams.topMargin + (int) offsetY;

        // 获取父容器的宽高
        int parentWidth = ((ViewGroup) rootView.getParent()).getWidth();
        int parentHeight = ((ViewGroup) rootView.getParent()).getHeight();

        // 计算姿态球的右边界和底边界
        int ballRight = leftMargin + rootView.getWidth();
        int ballBottom = topMargin + rootView.getHeight();

        // 限制姿态球的移动范围
        if ( leftMargin < 0 ) {
            leftMargin = 0;
        }
        if ( topMargin < 0 ) {
            topMargin = 0;
        }
        if ( ballRight > parentWidth ) {
            leftMargin = parentWidth - rootView.getWidth();
        }
        if ( ballBottom > parentHeight ) {
            topMargin = parentHeight - rootView.getHeight();
        }

        layoutParams.leftMargin = leftMargin;
        layoutParams.topMargin = topMargin;
        rootView.setLayoutParams(layoutParams);
    }

}
